﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Caching;

namespace PM.Code.Web
{
    /// <summary>
    /// 服务器的缓存帮助类cachehelper
    /// </summary>
    public class cachehelper
    {
        public cachehelper()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        ///<summary>
        ///设置当前应用程序指定CacheKey的Cache值,不设置时间
        ///</summary>
        ///<param name="key">Cache的唯一键</param>
        ///<param name="object">缓存内容</param>
        public static void SetCache(string key, object cachecontent)
        {
            if (key !="")
            {
                 try
                {
                    System.Web.HttpContext.Current.Cache.Insert(key, cachecontent);
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
            }
            else return;
        }
        ///<summary>
        ///设置当前应用程序指定CacheKey的Cache值,设置时间单位是秒
        ///</summary>
        ///<param name="key">Cache的唯一键</param>
        ///<param name="cachecontent">缓存内容</param>
        ///<param name="timeon">绝对过期时间</param>
        public static void SetCache(string key, object cachecontent,int timeon)
        {
            if (key != "")
            {
                try
                {
                    //设置缓存的绝对过期时间
                    System.Web.HttpContext.Current.Cache.Insert(key, cachecontent, null, DateTime.Now.AddSeconds((double)timeon), Cache.NoSlidingExpiration);
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
            }
            else
                return;
        }
        ///<summary>
        ///获取当前指定Key的Cache
        ///</summary>
        ///<param name="key">Cache的唯一键</param>
        ///<returns>返回Object</returns>
        public static object GetCache(string key)
        {
            if (key != "")
            {
                try
                {
                    return HttpContext.Current.Cache.Get(key);
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
            }
            else
                return null;
        }
    }
}
